/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts.action;

import com.myapp.struts.form.Estimate5bForm;
import com.myapp.util.DBConfig;
import com.myapp.util.DateTimeUtils;
import com.myapp.util.ValidateUtils;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Date;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.MapUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author toint
 * @since 12/2013
 * @version 1.0
 */
public class AddEstimate5bAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        Estimate5bForm frm = (Estimate5bForm) form;
        Map<String, String> mapUnit = frm.getMapUnit();
        Map<String, String> mapQuantity = frm.getMapQuantity();
        Map<String, String> mapSize = frm.getMapSize();
        Map<String, String> mapSpecifications = frm.getMapSpecifications();
        Map<String, String> mapPersonBenfit = frm.getMapPersonBenfit();
        Map<String, String> mapInternalForce = frm.getMapInternalForce();
        Map<String, String> mapStage = frm.getMapStage();
        Map<String, String> mapOther = frm.getMapOther();
        String strDate = frm.getEstamteDate();
        Date conDate = new Date();
        String parseDate = DateTimeUtils.convertDateTimeToString(conDate);
        Date estimateDate = DateTimeUtils.converDateForSql(parseDate);
        Integer[] arrWantId = frm.getArrWantId();
        
        for (int i = 0; i < arrWantId.length; i++) {
            Integer wantId = arrWantId[i];
            Float quantity = 0F;
            String specification = "";
            Float personBenfit = 0F;//thon
            Float internalForce = 0F;//noi luc
            Float state = 0F;//nha nuoc
            Float other = 0F;//nguon khac
            String unit = "";
            String size = "";
            Set enUnit = mapUnit.entrySet();
            Iterator iterUnit = enUnit.iterator();
            while(iterUnit.hasNext()) {
                Map.Entry entry = (Map.Entry) iterUnit.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key))
                    unit = str[0];
            }
            Set enQuantity = mapQuantity.entrySet();
            Iterator iterQuantity = enQuantity.iterator();
            while(iterQuantity.hasNext()) {
                Map.Entry entry = (Map.Entry) iterQuantity.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String strQuantity = str[0];
                    if (strQuantity != null && !"".equals(strQuantity))
                        quantity = Float.parseFloat(strQuantity);
                }
            }
            Set enSize = mapSize.entrySet();
            Iterator iterSize = enSize.iterator();
            while(iterSize.hasNext()) {
                Map.Entry entry = (Map.Entry) iterSize.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1))
                        size = str1;
                }
            }
            Set enSpec = mapSpecifications.entrySet();
            Iterator iterSpec = enSpec.iterator();
            while(iterSpec.hasNext()) {
                Map.Entry entry = (Map.Entry) iterSpec.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1))
                        specification = str1;
                }
            }
            Set enBen = mapPersonBenfit.entrySet();
            Iterator iterBen = enBen.iterator();
            while(iterBen.hasNext()) {
                Map.Entry entry = (Map.Entry) iterBen.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1))
                        personBenfit = Float.parseFloat(str1);
                }
            }
            Set enInter = mapInternalForce.entrySet();
            Iterator iterForce = enInter.iterator();
            while(iterForce.hasNext()) {
                Map.Entry entry = (Map.Entry) iterForce.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1))
                        internalForce = Float.parseFloat(str1);
                }
            }
            Set enState = mapStage.entrySet();
            Iterator iterState = enState.iterator();
            while(iterForce.hasNext()) {
                Map.Entry entry = (Map.Entry) iterState.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1))
                        state = Float.parseFloat(str1);
                }
            }
            Set enOther = mapOther.entrySet();
            Iterator iterOther = enOther.iterator();
            while(iterOther.hasNext()) {
                Map.Entry entry = (Map.Entry) iterOther.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1))
                        other = Float.parseFloat(str1);
                }
            }

            String sql = "insert into estimate_5b values (";
            sql += wantId + ", ";
            sql += "N'"+ unit +"', ";
            sql += quantity +", ";
            sql += "'"+ size +"', ";
            sql += "'"+ specification +"', ";
            sql += personBenfit + ", ";
            sql += internalForce + ", ";
            sql += state + ", ";
            sql += other + ", ";
            sql += 2 + ", ";
            sql += "'"+ new java.sql.Date(System.currentTimeMillis()) +"')";
            Connection conn = null;
            try {
                conn = DBConfig.getConnection();
                CallableStatement stm = conn.prepareCall(sql);
                stm.execute();
            } catch (Exception ex) {
                conn.close();
                System.err.println(ex);
                mapping.findForward("failed");
                break;
            }
        }
        
        return mapping.findForward("success");
    }
    
    
    
}
